package com.sell.sell.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sell.sell.domain.JsonData;
import com.sell.sell.domain.ProductCategory;
import com.sell.sell.domain.ProductInfo;
import com.sell.sell.mapper.ProductInfoMapper;
import com.sell.sell.service.ProductCategoryService;
import com.sell.sell.service.ProductInfoService;
import com.sell.sell.utils.ResultMsgUtil;
import com.sell.sell.utils.ResultVOUtil;
import com.sell.sell.vo.ProductInfoVO;
import com.sell.sell.vo.ProductVO;
import com.sell.sell.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/buyer/product")
@Slf4j
public class ProductCategoryController {
    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private ProductInfoService productInfoService;
    @RequestMapping("/add")
    public void addNew(ProductCategory productCategory){
//        ProductCategory productCategory = new ProductCategory();
//        productCategory.setCategoryName("汤匙");
//        productCategory.setCategoryType(2);
//        productCategory.setCreatedTime(new Date());
//        productCategory.setUpdateTime(new Date());
        productCategoryService.addNew(productCategory);
    }

    /**
     * 查询商品
     * @return
     */
    @RequestMapping("/selProducts")
    public  List<ProductCategory> selProducts(){
        List<ProductCategory>  data = productCategoryService.selProduct();
        return data;
    }
    @RequestMapping("/findOne")
    public  JsonData findOne(Integer categoryId){
        ProductCategory  data = productCategoryService.findOne(categoryId);
        return JsonData.buildSuccess(data,0);
    }
    @RequestMapping("/list")
    public ResultVO list(){
        /*1.查询所有上架商品*/
        List<ProductInfo> productInfoList = productInfoService.findUpAll();
        /*1.查询类目（一次性查询）*/
        /*lambda表达式*/
        List<Integer> categoryTypeList =productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> productCategoryList = productCategoryService.findCategoryByType(categoryTypeList);
        /*1.数据拼装*/
        List<ProductVO> productVOList = new ArrayList<>();
        for(ProductCategory productCategory:productCategoryList){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo:productInfoList){
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }
        return ResultVOUtil.success(productVOList);
    }

    /**
     * 分页查询
     */
    @Resource
    private ProductInfoMapper productInfoMapper;
    @RequestMapping("/findByPaging")
    public Object findByPaging(String productName, Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        Map param = new HashMap();
        param.put("productName","皮蛋粥");
        Page<ProductInfo> data = productInfoMapper.findAllByPaging(param);
        JSONObject result = new JSONObject();
        result.put("data",data);
        result.put("code",0);
        result.put("pages",data.getPages());
        result.put("total",data.getTotal());
        log.info("kkkkk:"+result);
//        return ResultMsgUtil.getMsg(result);
        return result;

    }

}
